      SUBROUTINE RDCAT1(LUCATLG,LUERR,IERR)
      IMPLICIT REAL*8 (A-H,O-Z)
C
C THIS IS A SERVICE ROUTINE FOR THE TARGET CATALOGUE READER, RDCAT.
C
C THIS ROUTINE READS THE ENTIRE TARGET CATALOGUE AND CHECKS EACH RECORD
C FOR COMPLIANCE WITH SOME REQUIREMENTS OF THE TOSS TARGET CATALOGUE
C FORMAT DESCRIPTION IN THE TOSS DUCUMENTATION LIBRARY. IF AN ERROR
C OCCURS, THE PROGRAM WILL STOP IN THIS ROUTINE TO PREVENT THE CALLING
C PROGRAM FROM USING POSSIBLY INVALID DATA. 
C
C CHECKS ARE DONE HERE BECAUSE THEY ARE EASY TO DO AND ARE THE SAME
C FOR ALL RECORDS IN THE FILE.
C
C     1. TARGET ID'S MUST BE IN ASCENDING ORDER
C     2. TARGET TYPE MUST BE 1, 2, ..., MAXTYPE.
C     3. THE RECORD MUST NOT HAVE MORE THAN MAXDATA DATA ITEMS; MAXDATA
C        IS A PARAMETER SET UP IN RDCAT.INC
C     4. TARGET ID MUST BE PRESENT
C     5. TARGET NAME MUST BE PRESENT
C     6. TARGET TYPE MUST BE PRESENT
C
C VARIABLE DIM TYPE I/O DESCRIPTION
C -------- --- ---- --- -----------
C
C LUCATLG   1   I*4  I  THE FORTRAN UNIT NUMBER OF THE TARGET CATALOGUE
C
C LUERR     1   I*4  I  THE FORTRAN UNIT NUMBER FOR ERROR MESSAGES.
C                       IF ZERO OR NEGATIVE, NO MESSAGES ARE POSSIBLE.
C
C IERR      1   I*4  O  ERROR RETURN FLAG.
C                       = 0, NO ERRORS.  =1, MAJOR ERRORS. 
C
C***********************************************************************
C
C BY C PETRUZZO, GSFC/742, 7/85.
C    MODIFIED....
C
C***********************************************************************
C
      INCLUDE 'RDCAT.INC'    ! SUPPLIES THE MAXDATA VALUE
      PARAMETER MAXDATA1 = MAXDATA + 1
      REAL*8 DUMDATA(MAXDATA1)
C
      CHARACTER*16 NAME,NAMETEST/'NOT PRESENT'/
      LOGICAL ERROR,ERROR1,ERROR2,ERROR3,ERROR4,ERROR5,ERROR6
      INTEGER KTEST/430711/
C
      IERR = 0
      ERROR =  .FALSE.
      KIDPREV = -99    
      REWIND LUCATLG
C
C
  100 CONTINUE
C
C INITIALIZE PARAMETERS TO BE READ SO WE WILL KNOW IF THERE WAS
C DATA IN THE CATALOGUE RECORD.
C
      KID =   KTEST
      NAME =  NAMETEST
      KTYPE = KTEST
      DUMDATA(MAXDATA1) = 'EMPTY'
C
C READ ONE RECORD.
C
      READ(LUCATLG,*,END=75) KID,NAME,KTYPE,DUMDATA
C
C SKIP ERROR CHECKS FOR RECORDS BEING IGNORED
C
      IF(KID.LE.0 .OR. KTYPE.LE.0) GO TO 100
C
C CHECK FOR TARGET ID'S IN ASCENDING ORDER
C
      ERROR1 = .FALSE.
      IF(KID.GT.KIDPREV) THEN
        KIDPREV = KID
      ELSE
        ERROR1 =.TRUE.
        IF(LUERR.GT.0) WRITE(LUERR,1050) 
     *       'TARGET ID VALUES ARE NOT IN ASCENDING ORDER.',KID,NAME
        KIDPREV = -99
        END IF
C
C CHECK FOR VALID TARGET TYPE AND WHETHER IT IS PRESENT
C
      ERROR2 = .FALSE.
      ERROR6 = .FALSE.
      IF(KTYPE.GT.MAXTYPE) THEN
        IF(KTYPE.NE.KTEST) THEN
          ERROR2 = .TRUE.
          IF(LUERR.GT.0) WRITE(LUERR,1050)
     *           'TARGET TYPE IS INVALID.',KID,NAME
        ELSE
          ERROR6 = .TRUE.
          IF(LUERR.GT.0) WRITE(LUERR,1050)
     *           'TARGET TYPE IS MISSING.',KID,NAME
          END IF
        END IF
C
C CHECK THAT NUMBER OF DATA ITEMS IS NOT GREATER THAN MAXDATA
C
      ERROR3 = .FALSE.
      IF(DUMDATA(MAXDATA1).NE.'EMPTY') THEN
        ERROR3 = .TRUE.
        IF(LUERR.GT.0) WRITE(LUERR,1050)
     *        'THERE ARE TOO MANY DATA ITEMS.',KID,NAME
        END IF
C
C CHECK FOR TARGET ID PRESENT
C
      ERROR4 = .FALSE.
      IF(KID.EQ.KTEST) THEN
        ERROR4 = .TRUE.
        KID = 0
        IF(LUERR.GT.0) WRITE(LUERR,1050)
     *        'TARGET ID IS MISSING.',KID,NAME
        END IF
C
C CHECK FOR TARGET NAME PRESENT
C
      ERROR5 = .FALSE.
      IF(NAME.EQ.NAMETEST) THEN
        ERROR5 = .TRUE.
        IF(LUERR.GT.0) WRITE(LUERR,1050)
     *         'TARGET NAME IS MISSING.',KID,NAME
        END IF
C
C SET GENERAL FLAG FOR ERROR ERRORS
C
      ERROR = ERROR  .OR. ERROR1 .OR. ERROR2 .OR. ERROR3 .OR.
     *                    ERROR4 .OR. ERROR5 .OR. ERROR6
C
C READ ANOTHER RECORD
C
      GO TO 100
C
C END-FILE PUT US HERE
C
   75 CONTINUE
C
C IF ERROR HAS BEEN FOUND, SET ERROR FLAG.
C
      IF(ERROR) THEN
        IERR = 1
        END IF
C
      RETURN
 1050 FORMAT(
     *  ' RDCAT1. ERROR IN TARGET CATALOGUE CONTENTS.'/,
     *  3X,A/,
     *  '   ERROR SENSED AT TARGET ID=',I7,'  TARGET NAME= ',A/)
      END
